package framework.utils
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Matrix;
import flash.geom.Rectangle;

public class BitmapUtil
{	
	public static const UNIT_W:int = 128;
	public static const UNIT_H:int = 128;
	
	public static const IMGAE_W:int = 256;
	public static const IMGAE_H:int = 256;
	
	public static const DIRTY_BITS:BitmapData = new BitmapData(1,1);
    
    public static function getBitmapData(source:DisplayObject, matrix:Matrix = null, rect:Rectangle = null):BitmapData
    {
        if (source.width == 0 || source.height == 0) return new BitmapData(1, 1);
		
		var bounds:Rectangle = source.getBounds(null);
		bounds.left = Math.floor(bounds.left);
		bounds.top = Math.floor(bounds.top);
		bounds.right = Math.ceil(bounds.right);
		bounds.bottom = Math.ceil(bounds.bottom);
		
        var bd:BitmapData = new BitmapData(bounds.width, bounds.height, true, 0);
        bd.draw(source, matrix, null, null, rect);
        return bd;
    }
	
	public static function drawByBitmapData(bd:BitmapData):DisplayObject
	{
		return new Bitmap(bd);
	}
	
	public static function copy(source:DisplayObject, matrix:Matrix = null, rect:Rectangle = null):DisplayObject
	{
		var bd:BitmapData = new BitmapData(source.width, source.height, true, 0);
		bd.draw(source, matrix, null, null, rect);
		
		return new Bitmap(bd);
	}
	
}
}